//    Copyright (C) Kherty.  All rights reserved.
using System;
using System.Diagnostics;
using System.IO.Packaging;
using OpenLS.Core.Serialization;
using OpenLS.Drawing.Media;
using OpenLS.Drawing.Serialization;

namespace OpenLS.Drawing
{
    public class Hyperlink : IOfficeXmlSerializableAs
    {
        private string _action;
        private bool _endSnd;


        private bool _highlightClick;
        private bool _history;
        private string _invalidUrl;
        private string tgtFrame;


        private string tooltip;
        private EmbeddedWavAudioFile _snd;
        private OfficeArtExtensionList extLst;
        private object target;
        //private string extervalValue;
        private string RelationshipType;

        public bool EndSounds
        {
            get { return _endSnd; }
            set { _endSnd = value; }
        }

        public bool HighlightClick
        {
            get { return _highlightClick; }
            set { _highlightClick = value; }
        }

        public string Tooltip
        {
            get { return tooltip; }
            set { tooltip = value; }
        }


        public string TargetFrame
        {
            get { return tgtFrame; }
            set { tgtFrame = value; }
        }

        public string Action
        {
            get { return _action; }
            set { _action = value; }
        }


        public string InvalidUrl
        {
            get { return _invalidUrl; }
            set { _invalidUrl = value; }
        }




        public bool History
        {
            get { return _history; }
            set { _history = value; }
        }

        #region IOfficeXmlSerializableAs Members

        void IOfficeXmlSerializableAs.ReadXmlAs(string name, ReadContext context)
        {
            using (var c = context.Read(name))
            {
                string rid = c.GetOptionalString(OfficeConstants.RelationshipNamespace + "id");
                if (!string.IsNullOrEmpty(rid ))
                {
                    var rel = c.Part.GetRelationship(rid);
                    if (rel.TargetMode == TargetMode.External)
                        this.target =  rel.TargetUri.ToString();
                    else
                    {
                        HyperlinkReader resolver = c.GetData<HyperlinkReader>();
                        var ss = PackUriHelper.ResolvePartUri(rel.SourceUri, rel.TargetUri).ToString();
                        resolver.AddAction(ss, delegate(object t) { this.target = t; });
                    }
                    this.RelationshipType = rel.RelationshipType;
                }
                _invalidUrl = c.GetOptionalString("invalidUrl", string.Empty);
                Action = c.GetOptionalString("action", string.Empty);
                tgtFrame = c.GetOptionalString("tgtFrame", string.Empty);
                tooltip = c.GetOptionalString("tooltip", string.Empty);
                _highlightClick = c.GetOptionalBoolean("highlightClick", false);
                _endSnd = c.GetOptionalBoolean("endSnd", false);
                History = c.GetOptionalBoolean("history", true);

                _snd = c.ReadOptionalElementAs<EmbeddedWavAudioFile>("snd");
                extLst = c.ReadOptionalElement<OfficeArtExtensionList>("extLst");
                

            }
        }

        void IOfficeXmlSerializableAs.WriteXmlAs(string name, WriteContext context)
        {
            using (WriteContext c = context.Write(DrawingConstants.DrawingNamespace + name ))
            {
                if (this.RelationshipType != null)
                {
                    //var s = extervalValue.Replace(" ", "%20").Replace("{", "%7b").Replace("{", "%7b").Replace("}", "%7d");
                    //var s = UriHelper.CleanupUri(extervalValue);
                    PackageRelationship rel;
                    if (target is string)
                        rel = c.CreateExternalRelationship((string)target, 
                                    RelationshipType);
                    else
                    {
                        var resolver = c.GetData<HyperlinkWriter>();
                        var value = resolver.GetValue(target);
                        rel = c.Part.CreateRelationship(new Uri(value, UriKind.RelativeOrAbsolute), TargetMode.Internal,
                                           RelationshipType);

                    }
                    c.SetString(OfficeConstants.RelationshipNamespace + "id", rel.Id);

                }
                c.SetOptionalString("invalidUrl", _invalidUrl, string.Empty);
                c.SetOptionalString("action", Action, string.Empty);
                c.SetOptionalString("tgtFrame", tgtFrame, string.Empty);
                c.SetOptionalString("tooltip", tooltip, string.Empty);
                c.SetOptionalBoolean("highlightClick", _highlightClick, false);
                c.SetOptionalBoolean("endSnd", _endSnd, false);
                c.SetOptionalBoolean("history", History, true);
                c.WriteOptionalElementAs(_snd, "snd");
                c.WriteOptionalElement(extLst);
            }
        }

        #endregion
    }
}